<?php
/*
* @version 2010-11-03
* @author mxrgus.pxrt
*/


/*
Forwards commands directly to SW through mod_sw.


Example:

    $supportworks   = new Supportworks();

    print_r($supportworks->connect());
    print_r($supportworks->get_last_response());

    $call = $supportworks->call('LOG CALL',

                    array(
                    "cust_id" => "mxrguspxrt",
                    "owner" => "mxrguspxrt",
                    "loggedby" => "mxrguspxrt",
                    "prob_text" => "APPPIIII",
                    "callclass" => "Incident",
                    "ria_environment" => "Test",
                    'priority' => '1',
                    'updatedb.updatetxt' => 'update'  # this goes to prob_text in first call 
                    )

    );
    print_r($supportworks->get_last_response());
*/



class Supportworks extends Object{

    // Config variables
    var $address = "";
    var $instance = "";
    var $instance_path = "";

    // Connection to the SW (remember, this contains information about who is doing actions
    var $connection = null;

    // Construct
    function __construct($address=SW_ADDRESS, $instance=SW_INSTANCE, $instance_path=SW_INSTANCE_PATH) {
        $this->address          = $address;
        $this->instance         = $instance;
        $this->instance_path    = $instance_path;
    }

    // Destruct
    function __destruct(){
        if($this->connection) $this->disconnect();
    }

    // Connect with SW server
    // @return int Connection ID or Fail code (greater than 5000 is failcode)
    function connect(){
        return $this->connection    = ($connection = swhd_wcopen($this->address, $this->instance, 5005)) ? $connection : null;
    }

    // Disconnect
    function disconnect() {
        return swhd_close($this->connection);
    }

    // Get last message from server
    function get_last_response() {
        // if theres no connection, no message can be received also
        return $this->connection ? swhd_getlastresponse($this->connection) : null;
    }

    // Calls function 
    // 
    // @returns array Puts return values from swhd_sendcommand, swhd_sendtextvalue and swhd_commit to array(command => output, args => array(arg1 => output, arg2 => output), commit => output)
    function call($command, $args) {
        // return values - array(return=>..., output=>...)
        $return                     = array('return'=>array(), 'output'=>array());

        // prepare call function
        $return['return']['command']    = swhd_sendcommand($this->connection, $command);
        $return['output']['command']    = $this->get_last_response();
        // prepare set args
        $return['return']['arg']        = array();
        $return['output']['arg']        = array();
        foreach ($args as $key => $value) {
            $return['return']['arg'][$key]      = swhd_sendtextvalue($this->connection, $key, $value);
            $return['output']['arg'][$key]      = $this->get_last_response();
        }
        // commit function
        $return['return']['commit']      = swhd_commit($this->connection);
        $return['output']['commit']      = $this->get_last_response();

        // logging info error
        if(!(bool)$return['return']['command']){    Logger::error(array($_SERVER['REMOTE_ADDR'].' Unable to execute command' => array($command, $args, $return['output']['command']) ));    } 
        if(!(bool)$return['return']['commit']){     Logger::error(array($_SERVER['REMOTE_ADDR'].' Unable to commit command' => array($command, $args, $return['output']['commit']) ));      } 
        

        // return info
        return $return;
    }

}





